﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.BusinessRule.System;
using Jumpton.SFA.Server.BusinessRule;
using Jumpton.SFA.Server.Entity;

public partial class Customer_Select : PageBase
{
    private const string NEW_PAGE = "Order_Create.aspx?customerID={0}&userID={1}&chainID={2}";

    protected void Page_Load(object sender, EventArgs e)
    {
        //if (this.txtHidOrg.Value == "")
        //{
        //    this.txtHidOrg.Value = OrgHidValue;
        //    this.txtOrg.Text = OrgShowValue;
        //    OrgHelper.BindChiansDDL(MyStateManager.DomainID, this.txtHidOrg.Value, this.ddlChains);
        //    OrgHelper.BindUsersDDL(MyStateManager.DomainID, this.txtHidOrg.Value, this.ddlUsers);

        //}

        if (!IsPostBack)
        {
            DataBind();
            if (txtHidOrg.Value == "")
            {
                this.txtHidOrg.Value = OrgHidValue;
                this.txtOrg.Text = OrgShowValue;
                OrgHelper.BindChiansDDL(MyStateManager.DomainID, this.txtHidOrg.Value, this.ddlChains);
              
            }
            
            InitForm();
        }
    }

    private void InitForm()
    {
        DictionaryItemsManager dim = new DictionaryItemsManager();

        //销售渠道
        ddlChannel.DataTextField = "Name";
        ddlChannel.DataValueField = "DictionaryItemId";
        ddlChannel.DataSource = dim.GetItemList(DictionaryEnum.CustomerChannel, this.MyStateManager.DomainID);
        ddlChannel.DataBind();
        ddlChannel.Items.Insert(0, new ListItem("--所有--", ""));

        USER_CHAINS userChains = new UserManager().LoadChainsByUser(MyStateManager.UserID);
        
        //经销商用户
        if (userChains != null)
        {
            txtOrg.Text = new OrgainzationManager().Load(MyStateManager.OrgID).NAME;
            txtHidOrg.Value = "'" + MyStateManager.OrgID + "'";
            OrgHelper.BindChiansDDL(MyStateManager.DomainID, txtHidOrg.Value, ddlChains);
            ddlChains.SelectedValue = userChains.CHAINSID.ToString();
            ddlChains.Enabled = false;
            
        }


    }

    private void BindGrid()
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();

        if (txtHidOrg.Value != string.Empty)
        {
            sb.Append(string.Format(" AND C.ORG_ID IN ({0})", txtHidOrg.Value));

        }

        ////经销商
        //if (ddlChains.SelectedValue != string.Empty)
        //{
        //    sb.Append(" And CHAINS.ID='" + ddlChains.SelectedValue + "' ");
        //}
        
      
        //渠道
        if (ddlChannel.SelectedValue != string.Empty)
        {
            sb.Append(" AND LOCAL_LEVEL1_CODE =  '" + ddlChannel.SelectedValue + "'  ");
        }

        //门店名称
        if (txtCustomer.Text.Trim() != string.Empty)
        {
            sb.Append("  AND (C.[CODE] LIKE '%" + txtCustomer.Text.Trim() + "%' or C.[NAME] LIKE '%" + txtCustomer.Text.Trim() + "%' or CI.[ADDRESS_LINE] like  '%" + txtCustomer.Text.Trim() + "%' "
                + " or C.default_ship_to_code LIKE '%" + txtCustomer.Text.Trim() + "%'  or  "
                + " CI.telephone LIKE '%" + txtCustomer.Text.Trim() + "%' or  CI.fax LIKE '%" + txtCustomer.Text.Trim() + "%' "
                + " )     ");
        }
        sb.Append(" AND RC.USER_ID=" + MyStateManager.UserID);

        DataSet ds = OrderHeaderManager.SearchCustomer(sb.ToString());
        if (ds != null && ds.Tables.Count > 0)
        {
            gvCustomerList.DataSource = ds.Tables[0];
            gvCustomerList.DataBind();
        }


    }
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        BindGrid();
    }
    protected void gvCustomerList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            try
            {
                Grid_RowDataBound(sender,e);
                ((RadioButton)e.Row.FindControl("RadioButton1")).Attributes.Add("onclick", "setRadio(this)");
            }
            catch (Exception)
            { }
        }
    }
    protected void gvCustomerList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvCustomerList.PageIndex = e.NewPageIndex;
        BindGrid();
    }
    protected void txtOrg_TextChanged(object sender, EventArgs e)
    {
        ddlChains.Items.Clear();
       

        if (!string.IsNullOrEmpty(txtHidChains.Value))
        {
            string[] options = txtHidChains.Value.Split('∮');


            for (int i = 0; i < options.Length; i++)
            {
                string[] item = options[i].Split('∞');
                ddlChains.Items.Add(new ListItem(item[1], item[0]));
            }


        }

        //判断当前的人所在的部门是不是最小的一级org
        var data = new OrgainzationManager().SearchOrgTree(MyStateManager.DomainID, MyStateManager.OrgID, null);
        if (data.Rows.Count != 1)
        {
            if (!string.IsNullOrEmpty(txtHidUsers.Value))
            {
                string[] options2 = txtHidUsers.Value.Split('∮');
                for (int i = 0; i < options2.Length; i++)
                {
                    string[] item2 = options2[i].Split('∞');
                  
                }
            }
          
        }
        
        ddlChains.Items.Insert(0, new ListItem("--所有--", ""));
        

    }
    protected void gvCustomerList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Sel")
        {
            decimal  chainID;
           
            decimal.TryParse(ddlChains.SelectedValue, out chainID);

            Response.Redirect(string.Format(NEW_PAGE, e.CommandArgument.ToString(), -1,chainID));

        }
    }
}
